'use strict';

angular.module('app')
    .controller('cacheCtrl', ['$scope', '$rootScope', '$state', '$timeout',
        function($scope, $rootScope, $state, $timeout) {
            var title = "重置密码缓冲";
            var defaultAva = $rootScope.defaultAvatar;
            $scope.myImage='';
            var user={};
            user.account=$state.params.account;
            user.UUID=$state.params.uuid;
            // $scope.myCroppedImage=$scope.myCroppedImage ;
            $scope.myCroppedImage = '';
            $scope.title = $rootScope.title = title;
            $scope.loading = true;
            function ValidateURL(){
                //$scope.loading = true;
                if(user){
                    $scope.isDisabled = true;//提交disabled
                    $.ajax({
                        type: 'POST',
                        url : '/user/validateURL',
                        dataType:'json',
                        contentType:'application/json;charset-UTF-8',
                        data:angular.toJson(user)
                    }).then(callback);
                }
                function callback(result){
                    if(result.httpCode ==200){//成功
                        $state.go('access.resetPassword',{account:user.account});

                    }else{
                        alert("当前链接已经过期！");
                        //$state.go('main.sys.user.baseInfoUpdate');
                    }
                }
            };
            ValidateURL();
            var handleFileSelect=function(evt) {
                var file=evt.currentTarget.files[0];
                if(!/image\/\w+/.test(file.type)){
                    return false;
                }
                var reader = new FileReader();
                reader.onload = function (evt) {
                    $scope.$apply(function($scope){
                        $scope.myImage=evt.target.result;
                    });
                };
                reader.readAsDataURL(file);
            };
            angular.element(document.querySelector('#fileInput')).on('change',handleFileSelect);
            $('.upload-pic-box a').click(function() {
                $('#fileInput').click();
            });
            //表单验证
            function validate(userId){
                //notEqual 规则
                $.validator.addMethod('notEqual', function(value, ele){
                    return value != this.settings.rules[ele.name].notEqual;
                });
                jQuery('form').validate({
                    rules: {
                        password:{
                            maxlength: 16
                        },
                        confirmPassword:{
                            // required: true,
                            maxlength: 16,
                            equalTo: "#password"
                        }
                    },
                    messages: {
                        password:{
                            //required: '请填写密码',
                            maxlength: '密码长度不可大于16位'
                        },
                        confirmPassword:{
                            //required: '请填写确认密码',
                            maxlength: '密码长度不可大于16位',
                            equalTo: '两次输入的密码不相符'
                        }
                    },
                    submitHandler: function() {
                        $scope.submit();
                    }
                });
            }

        }]);